package org.androidpn.activity;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.androidpn.HospitalClientApplication;
import org.androidpn.R;
import org.androidpn.http.HttpApi;
import org.androidpn.model.NetModel;
import org.androidpn.utils.ConstanceUtil;
import org.androidpn.utils.JsonUtil;
import org.androidpn.utils.ToastUtil;

import com.android.volley.VolleyError;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class RegisterActivity extends BaseActivity implements View.OnClickListener {

	private ImageView ivBack;
	private EditText etPhone;
	private EditText etPwd1;
	private EditText etPwd2;
	private TextView tvGetcode;
	private int type = ConstanceUtil.TYPE_BINGREN;
	String securityCode = "";
	RadioGroup radioGroup;

	// 消息提示
	private static final int MSG_LOGIN_SHOW_LOADING = 0x301;
	private static final int MSG_LOGIN_HIDE_LOADING = 0x302;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_register);
		ivBack = (ImageView) findViewById(R.id.iv_back);
		etPhone = (EditText) findViewById(R.id.et_phone);
		tvGetcode = (TextView) findViewById(R.id.tv_getcode);
		radioGroup = (RadioGroup) findViewById(R.id.radio_group);
		etPwd1 = (EditText) findViewById(R.id.et_pwd1);
		etPwd2 = (EditText) findViewById(R.id.et_pwd2);
		ivBack.setOnClickListener(this);
		tvGetcode.setOnClickListener(this);
		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup arg0, int arg1) {
				// TODO Auto-generated method stub
				int radioButtonId = arg0.getCheckedRadioButtonId();
				if (radioButtonId == R.id.rd_man) {
					type = ConstanceUtil.TYPE_BINGREN;
				} else {
					type = ConstanceUtil.TYPE_YISHENG;
				}
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_register, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();

		// noinspection SimplifiableIfStatement
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.iv_back: {
			finish();
			break;
		}
		case R.id.tv_getcode:
			String pwd1 = etPwd1.getText().toString();
			String pwd2 = etPwd2.getText().toString();
			if (isMobileNO(etPhone.getText().toString())) {
				if (TextUtils.isEmpty(pwd1) || TextUtils.isEmpty(pwd1) || !pwd1.equalsIgnoreCase(pwd2)) {
					ToastUtil.showShortToast(this, "输入密码有误");
					return;
				}

				sendEmptydMessage(MSG_LOGIN_SHOW_LOADING);
				Random random = new Random();
				securityCode = "";
				for (int i = 0; i < 4; i++) {
					securityCode += random.nextInt(10);
				}
				MyResponseListener listener = new MyResponseListener();
				HttpApi.sendIndentifyCode(etPhone.getText().toString(), securityCode, listener, listener);
				// Intent intent = new Intent(RegisterActivity.this,
				// SubmitSecurityCodeActivity.class);
				// intent.putExtra("phone_number",
				// etPhone.getText().toString());
				// intent.putExtra("type", type);
				// intent.putExtra("password", etPwd1.getText().toString());
				// intent.putExtra("code", securityCode);
				// startActivity(intent);

			} else {
				ToastUtil.showShortToast(this, "输入的电话号码不正确");
			}
			break;
		}
	}

	public boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);
		return m.matches();
	}

	public class MyResponseListener
			implements com.android.volley.Response.Listener<String>, com.android.volley.Response.ErrorListener {

		@Override
		public void onResponse(String response) {
			// 隐藏提示框
			sendEmptydMessage(MSG_LOGIN_HIDE_LOADING);
			NetModel netModel = JsonUtil.parseObject(response, NetModel.class);

			ToastUtil.showShortToast(HospitalClientApplication.getmInstance(), netModel.getContent());
			if (netModel.getCode() == ConstanceUtil.REQUEST_ALREADY_EXIST) {
				return;
			} else if (netModel.getCode() == ConstanceUtil.REQUEST_SUCCESS) {
				Intent intent = new Intent(RegisterActivity.this, SubmitSecurityCodeActivity.class);
				intent.putExtra("phone_number", etPhone.getText().toString());
				intent.putExtra("type", type);
				intent.putExtra("password", etPwd1.getText().toString());
				intent.putExtra("code", securityCode);
				startActivity(intent);
				finish();
			} else {
				return;
			}
		}

		@Override
		public void onErrorResponse(VolleyError error) {
			// 隐藏提示框
			sendEmptydMessage(MSG_LOGIN_HIDE_LOADING);
			ToastUtil.showShortToast(HospitalClientApplication.getmInstance(), "网络异常");

		}
	}

	@Override
	protected void onHandleMessage(Message msg) {
		switch (msg.what) {
		case MSG_LOGIN_SHOW_LOADING: {
			showProgressDialog(getString(R.string.save_loading));
			break;
		}
		case MSG_LOGIN_HIDE_LOADING: {
			hideProgressDialog();
			break;
		}
		default:
			break;
		}
	}
}
